package com.itfreer.gis.server.arcgis;

import java.io.File;

import com.itfreer.gis.server.utils.webp.WebpConvert;

import io.minio.MinioClient;
import io.minio.policy.PolicyType;

/**
 * 定义ArcGIS WMTS切片数据上传代码
 */
public class ArcGISWMTSConvert {

	private String wmtsBucket = "wmts";
	private String wmtsBasePath = "F:\\TEMP\\arcgistemp\\jtjs_jtjs15\\图层\\_alllayers";
	private String wmtsName = "hn_clx_yx/v2.0";

	public void uploader(boolean convertWeb) {
		try {
			File baseFile = new File(wmtsBasePath);
			// 1、层级
			File[] cjList = baseFile.listFiles();
			if (cjList == null) {
				return;
			}

			// 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
			MinioClient minioClient = new MinioClient("http://47.106.79.19:9000", "minio", "123!@#qweQWE@ITFREER");

			// 检查存储桶是否已经存在
			boolean isExist = minioClient.bucketExists(wmtsBucket);
			if (!isExist) {
				// 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
				minioClient.makeBucket(wmtsBucket);
				minioClient.setBucketPolicy(wmtsBucket, "*", PolicyType.READ_WRITE);
			}

			for (int i = 0; i <= cjList.length - 1; i++) {
				File cj = cjList[i];
				String cjName = cj.getName();
				String cjn = cjName.substring(1);
				if (cjn.equals("DS_Store")) {
					continue;
				}
				Integer cji = Integer.parseInt(cjn);
//				if (i > 2) {
//					System.out.println(4);
//					return;
//				}
				// 2、空目录
				File[] knList = cj.listFiles();
				if (knList != null) {
					for (int j = 0; j < knList.length; j++) {
						File kn = knList[j];
						// 3、图片
						File[] pngList = kn.listFiles();
						if (pngList == null) {
							continue;
						}
						String xName = kn.getName().substring(1);// 获取ArcGIS的x坐标(16进制)
						if (pngList != null) {
							// 分行处理
							if (convertWeb) {
								File[] webpList = new File[pngList.length];
								WebpConvert.encodingToWebp(pngList, webpList, 3);
								pngList = webpList;
							}

							for (int k = 0; k < pngList.length; k++) {
								File webpFile = pngList[k];
								// 上传webp到服务器
								String[] webps = webpFile.getName().split("_");
								String[] hz = webps[0].split("\\.");
								String yName = hz[0].substring(1);// 获取ArcGIS的y坐标(16进制)
								Integer x = Integer.parseInt(xName, 16);// 转换10进制x坐标
								Integer y = Integer.parseInt(yName, 16);// 转换10进制x坐标
								String key = wmtsName + "/" + cji.toString() + "/" + y.toString() + "/" + x.toString()
										+ "." + hz[1];
								System.out.println("当前上传——级别(" + (i + 1) + "/" + cjList.length + ")：" + cji + "————X坐标("
										+ (j + 1) + "/" + knList.length + ")：" + x + "/" + xName + "————Y坐标(" + (k + 1)
										+ "/" + pngList.length + ")：" + y + "/" + yName);
								if (convertWeb) {
									minioClient.putObject(wmtsBucket, key, webpFile.getPath(), "image/webp");
								} else {
									minioClient.putObject(wmtsBucket, key, webpFile.getPath(), "image/png");
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			System.out.println("Error occurred: " + e);
			e.printStackTrace();
		}
	}
}
